package com.letfox.finance.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.letfox.finance.dao.AccountRlInoutDao;
import com.letfox.finance.dao.AccountRlNoviceDao;
import com.letfox.finance.dao.IncomeDetailDao;
import com.letfox.finance.dao.NoviceInfoDao;
import com.letfox.finance.dao.NoviceRlInoutDao;
import com.letfox.finance.dao.OutcomeDetailDao;
import com.letfox.finance.enums.FINInoutTypeEnums;
import com.letfox.finance.model.FINAccountRlInout;
import com.letfox.finance.model.FINAccountRlNovice;
import com.letfox.finance.model.FINIncomeDetail;
import com.letfox.finance.model.FINNoviceInfo;
import com.letfox.finance.model.FINNoviceRlInout;
import com.letfox.finance.model.FINOutcomeDetail;
import com.letfox.finance.service.ComprehensiveService;

/**
 * 综合业务操作
 * @author boy.zhao
 *
 */
@Service
public class ComprehensiveServiceImpl implements ComprehensiveService {

	@Autowired
	private IncomeDetailDao incomeDetailDao;
	@Autowired
	private AccountRlInoutDao accountRlInoutDao;
	@Autowired
	private OutcomeDetailDao outcomeDetailoDao;
	@Autowired
	private NoviceRlInoutDao noviceRlInoutDao;
	@Autowired
	private NoviceInfoDao noviceInfoDao;
	@Autowired
	private AccountRlNoviceDao  accountRlNoviceDao;
	
	/**
	 * 根据时间段查询充值和累计充值统计
	 */
	@Override
	public Map<String,Object> periodTime_incomeDetail(String finAccountId,Date startTime,Date endTime) {
		
		Map map = new HashMap<String,Object>();
		//这段时间的收入
		BigDecimal totalIncome = new BigDecimal("0.000");
		//这段时间的支出
		BigDecimal totalOutcome = new BigDecimal("0.000");
		
		//这段时间的收入明细
		List<FINIncomeDetail> finIncomeDetails = new ArrayList<FINIncomeDetail>();
		//这段时间的支出明细
		List<FINOutcomeDetail> finOutcomeDetails = new ArrayList<FINOutcomeDetail>();
		
		Map parametermap = new HashMap();
		parametermap.put("finAccountId", finAccountId);
		parametermap.put("startTime", startTime);
		parametermap.put("endTime", endTime);
		List<FINAccountRlInout> accountIns = accountRlInoutDao.periodTime_incomeDetail(parametermap);
		if(accountIns != null && accountIns.size() != 0){
			for (FINAccountRlInout finAccountRlInout : accountIns) {
				//收入明细
				if(finAccountRlInout.getFinInoutType().equals(FINInoutTypeEnums.FININOUTTYPE.INCOME.getValue())){
					FINIncomeDetail finIncomeDetail = incomeDetailDao.selectByPrimaryKey(finAccountRlInout.getFinInoutId());
					if(finIncomeDetail != null){
						totalIncome = totalIncome.add(finIncomeDetail.getFinInAmount());
						finIncomeDetails.add(finIncomeDetail);
					}
				}
				
		
				//支出明细
				if(finAccountRlInout.getFinInoutType().equals(FINInoutTypeEnums.FININOUTTYPE.OUTCOME.getValue())){
					FINOutcomeDetail finOutcomeDetail = outcomeDetailoDao.selectByPrimaryKey(finAccountRlInout.getFinInoutId());
					if(finOutcomeDetail != null){
						totalOutcome = totalOutcome.add(finOutcomeDetail.getFinOutAmount());
						finOutcomeDetails.add(finOutcomeDetail);
					}
				}
			}			
			map.put("finOutcomeDetails", finOutcomeDetails);
			map.put("totalOutcome", totalOutcome.toString());
		}
		
		map.put("finIncomeDetails", finIncomeDetails);
		map.put("totalIncome", totalIncome.toString());
		
		return map;
	}
	/**
	 * 开发票保存
	 * 返回值 1 为成功存入数据 否则  -1 存入失败
	 */
	@Override
	public int provide_Invoice(FINNoviceInfo fINNoviceInfo,FINNoviceRlInout finNoviceRlInout){
		
		int noviceRlInout_Int = noviceRlInoutDao.insertSelective(finNoviceRlInout);
		if(noviceRlInout_Int == 1){
			int noviceInf_Int = noviceInfoDao.insertSelective(fINNoviceInfo);
			if(noviceInf_Int == 1){
				return 1;
			}
			return -1;
		}
		return -1;
		
	}

	
	/**
	 * 开发票历史记录
	 */
	@Override
	public List Invoice_history(String finAccountId){
		List<FINNoviceInfo> noviceInfos = new ArrayList<FINNoviceInfo>();
		List<FINAccountRlNovice>  accountRlNovices = accountRlNoviceDao.select_finAccountId(finAccountId);
		
		for (FINAccountRlNovice finAccountRlNovice : accountRlNovices) {
			FINNoviceInfo finNoviceInfo= noviceInfoDao.selectByPrimaryKey(finAccountRlNovice.getFinNoviceId());
			if(finNoviceInfo==null){
				noviceInfos.add(finNoviceInfo);
			}
		}
		return noviceInfos;
		
	}
}
